#include "lightMessage.h"
#include <QLabel>
#include <QTimer>
#include <QPropertyAnimation>
#include <QDesktopWidget>

LightMessage::LightMessage(QWidget *parent,QString info, QString strFontSize, QString strColor) : QFrame(parent)
{
    //窗口设置
    //int width = 200;
    //int height = 134;
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    this->setAttribute(Qt::WA_TranslucentBackground); //设置窗体背景透明
    this->resize(200,134);
    //刷背景图片
    QLabel *pLabelImg = new QLabel(this);
    pLabelImg->resize(width(),height());
    pLabelImg->setGeometry(0,0,width(),height());
    QImage img(":/new/prefix1/resource/lightMessage.png");
    QImage img2 = img.scaled(width(),height(),Qt::KeepAspectRatio);
    pLabelImg->setPixmap(QPixmap::fromImage(img2));
    //刷文字
    QLabel *pLabelStr = new QLabel(this);
    pLabelStr->setAlignment(Qt::AlignCenter);
    pLabelStr->setStyleSheet(QString("QLabel{color:%1;font:%2 SimHei;border-radius:5px;}")
                             .arg(strColor).arg(strFontSize));

    pLabelStr->setText(info);
    pLabelStr->adjustSize();
    pLabelStr->setGeometry(width()/25,height()/13.5,width()/1.45,height()/2.4);
}

void LightMessage::information(QWidget *parent,QString info, int time, QString strFontSize, QString strColor)
{
    LightMessage *widgetPtr = new LightMessage(parent,info,strFontSize,strColor);
    if(parent)
    {
        int x = parent->pos().x();
        int y = parent->pos().y() - widgetPtr->height();
        widgetPtr->move(x,y-2);
    }
    if(time < 1100) time = 1100;
    QTimer::singleShot(time-1000,widgetPtr,[=]()
    {
        QPropertyAnimation *pAnimation = new QPropertyAnimation(widgetPtr,"windowOpacity",widgetPtr);
        pAnimation->setDuration(1000);
        pAnimation->setEasingCurve(QEasingCurve::InCirc);
        pAnimation->setStartValue(1.0);
        pAnimation->setEndValue(0.0);
        pAnimation->start();
        connect(pAnimation,&QPropertyAnimation::finished,[=]{
            delete widgetPtr;
        });
    });
    widgetPtr->show();
}
#include <QMouseEvent>
void LightMessage::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    this->close();
}
